﻿package cn.edu.zjusc.Webservice;

import java.util.List;

import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.jboss.wsf.spi.annotation.WebContext;


import cn.edu.zjusc.POJO.OnlineRegisterAdmin;
import cn.edu.zjusc.Security.ProfileInterceptor;
import cn.edu.zjusc.Security.UserCave;

@Stateless
@WebService( name = "OnlineRegisterAdminService", serviceName = "OnlineRegisterAdminService")   
@SOAPBinding(style = SOAPBinding.Style.RPC)
@WebContext (contextRoot="/ZJUSCIX/Service", urlPattern="/OnlineRegisterAdminService") 
public class OnlineRegisterAdminService {
	
	
	/**
	 * 获得所有管理员
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<OnlineRegisterAdmin> GetAllOnlineRegisterAdminInfo() {

		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();

		Query query=em.createQuery("from OnlineRegisterAdmin e");
		List onlineRegisterAdminList=query.getResultList();
			int listSize=onlineRegisterAdminList.size();
			for(int i=0;i<listSize;i++){
			((OnlineRegisterAdmin) onlineRegisterAdminList.get(i)).setPassword("391732016");
				System.out.println("password:"+((OnlineRegisterAdmin) onlineRegisterAdminList.get(i)).getPassword());
			}
		em.close();
		emf.close();
		return onlineRegisterAdminList;
	}
	
	
	/**
	 * 删除管理员
	 * @param uc
	 * @param ID
	 * @return
	 */
	@Interceptors(ProfileInterceptor.class)
	public String DeleteOnlineRegisterAdminInfo(@WebParam(name = "uc") UserCave uc,@WebParam(name = "id") int ID) {
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		//开始事务
		em.getTransaction().begin();
		OnlineRegisterAdmin eiBean=em.find(OnlineRegisterAdmin.class, ID);
		em.remove(eiBean);
		//提交事务
		em.getTransaction().commit();
		
		em.close();
		emf.close();
		
		return "删除用户信息成功";
	}
	
	/**
	 * 修改管理员信息
	 * @param uc
	 * @param adBean
	 * @return
	 */
	@Interceptors(ProfileInterceptor.class)
	public String ModifyOnlineRegisterAdminInfo(@WebParam(name = "uc") UserCave uc,@WebParam(name="adminEntry")OnlineRegisterAdmin adBean){
		
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
			if(!adBean.getPassword().equals("391732016")){
				//开始事务
				em.getTransaction().begin();
				em.merge(adBean);
				//提交事务
				em.getTransaction().commit();
			}else {
				//开始事务
				em.getTransaction().begin();
				OnlineRegisterAdmin adminInfo= em.find(OnlineRegisterAdmin.class, adBean.getId());
				adminInfo.setUsername(adBean.getUsername());
				//提交事务
				em.getTransaction().commit();
			}
		em.close();
		emf.close();
		return "修改用户信息成功";
	}
	
	/**
	 * 添加新的管理员
	 * @param uc
	 * @param adBean
	 * @return
	 */
	@Interceptors(ProfileInterceptor.class)
	public String NewOnlineRegisterAdminInfo(@WebParam(name = "uc") UserCave uc,@WebParam(name="adminEntry")OnlineRegisterAdmin adBean){
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
	
		//开始事务
		em.getTransaction().begin();
			
		em.merge(adBean);
		//提交事务
		em.getTransaction().commit();
		
		em.close();
		emf.close();
		return "新增用户成功";
		
	}
}
